//
//  FLGuidePageView.m
//  Follow
//
//  Created by liangshangjia on 16/5/9.
//  Copyright © 2016年 Sami-industry. All rights reserved.
//

#import "FLGuidePageView.h"
#import "UIImage+Initial.h"


@implementation FLGuidePageView{
    
    CGFloat      _topMargin_icon;
    CGFloat      _topMargin_word;
    int          _pageIndex;
    
    UIImageView *_iconImageView;
    
}



+ (instancetype)guidePageWithTopMargin_icon:(CGFloat)topMargin_icon andTopMargin_word:(CGFloat)topMargin_word withPage:(int)pageIndex
{
    
    FLGuidePageView *page = [[self alloc]init];
    [page setValue:@(topMargin_icon) forKey:@"_topMargin_icon"];
    [page setValue:@(topMargin_word) forKey:@"_topMargin_word"];
    [page setValue:@(pageIndex) forKey:@"_pageIndex"];
    
    [page configure];
    
    
    return page;
}


- (void)configure
{
    _iconImageView = [UIImageView new];
    UIImage *icon = [UIImage imageNamed:[NSString stringWithFormat:@"ic_guide_%d.jpg",_pageIndex+1]];
    [_iconImageView setImage:icon];
    [self addSubview:_iconImageView];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@(icon.size.height/icon.size.width*SM_SCREEN_WIDTH));
    }];
}


- (void)showAnimationWhenDidAppear
{
    [self addScaleAnimationWithFromValue:@(.9f) toValue:@(1.f) onView:_iconImageView];
    /*
    if (!_bottomButton) {
        return;
    }
    
    
    if (_bottomButton.hidden) {
        _bottomButton.hidden = NO;
        _bottomButton.alpha  = 0;
        [UIView animateWithDuration:.5 animations:^{
            _bottomButton.alpha = 1;
        }];
    }else {
        [self addShakeAnimationOnView:_bottomButton];
    }
     */
}


- (void)showAnimationWhenDidDisappear
{
    [self addScaleAnimationWithFromValue:@(1.f) toValue:@(.9f) onView:_iconImageView];
}



- (void)addScaleAnimationWithFromValue:(NSNumber *)fromValue toValue:(NSNumber *)toValue onView:(UIView *)view
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.fromValue         = fromValue;
    animation.toValue           = toValue;
    animation.duration          = kFLGuideViewDuration;
    animation.timingFunction    = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    view.layer.transform  = CATransform3DMakeScale([toValue floatValue], [toValue floatValue], 1);
    [view.layer addAnimation:animation forKey:nil];
}

- (void)addShakeAnimationOnView:(UIView *)view
{
    CAKeyframeAnimation *animation3 = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    animation3.duration             = kFLGuideViewDuration;
    animation3.values               = @[@(angle2Radian(0)),@(angle2Radian(3)),@(angle2Radian(0)),@(angle2Radian(3)),@(angle2Radian(0))];
    animation3.fillMode             = kCAFillModeForwards;
//    view.layer.anchorPoint    = CGPointMake(.5, 1); //设置转动支点(默认为view的中心)
    [view.layer addAnimation:animation3 forKey:nil];
}


#pragma mark - Actions
- (void)didClickBottomButton:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(guidePage:didClickBottomButton:)]) {
        [self.delegate guidePage:self didClickBottomButton:sender];
    }
}
@end
